package com.airsystem.pos.bengkel.controller.report;

import java.awt.Frame;
import java.io.InputStream;
import java.util.List;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.Karyawan;
import com.airsystem.pos.bengkel.interfaces.IReport;
import com.airsystem.pos.bengkel.model.report.EmployeeReportModel;
import com.airsystem.pos.bengkel.service.LaporanBulananServices;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class EmployeeReportController extends EmployeeReportModel implements IReport {
	private static final Logger LOG = Logger.getLogger(EmployeeReportController.class.getSimpleName());

	private LaporanBulananServices laporanBulananServices = new LaporanBulananServices();

	public EmployeeReportController() {
		super();
	}

	public void print() {
		try {
			List<Karyawan> karyawans = laporanBulananServices.print();

			JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(karyawans);

			InputStream inputStream = EmployeeReportController.class.getResourceAsStream("/reports/EmployeeReport.jasper");

			JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);

			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, beanCollectionDataSource);

			jasperViewer = new JasperViewer(jasperPrint, false);
			jasperViewer.setTitle("Laporan Data Karyawan");
			jasperViewer.setExtendedState(Frame.MAXIMIZED_BOTH);
			jasperViewer.setVisible(true);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(EmployeeReportController.this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}